package cn.heygod.graywhale.modules.ipaas.controller;

import cn.heygod.graywhale.modules.ipaas.service.GwScriptService;
import com.github.pagehelper.PageInfo;
import cn.heygod.graywhale.common.context.UserContext;
import cn.heygod.graywhale.modules.ipaas.domain.GwScript;
import cn.heygod.graywhale.common.tools.string.Convert;
import cn.heygod.graywhale.common.web.base.BaseController;
import cn.heygod.graywhale.common.web.domain.request.PageDomain;
import cn.heygod.graywhale.common.web.domain.response.Result;
import cn.heygod.graywhale.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 脚本管理Controller
 *
 * @author simon
 * @date 2023-11-30
 */
@RestController
@RequestMapping("/ipaas/script")
public class GwScriptController extends BaseController {

    private String prefix = "ipaas/script";

    @Autowired
    private GwScriptService gwScriptService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/ipaas/script/main','ipaas:script:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询脚本管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/ipaas/script/data','ipaas:script:data')")
    public ResultTable list(@ModelAttribute GwScript gwScript, PageDomain pageDomain) {
        PageInfo<GwScript> pageInfo = gwScriptService.selectGwScriptPage(gwScript, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增脚本管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/ipaas/script/add','ipaas:script:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增脚本管理
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/ipaas/script/add','ipaas:script:add')")
    public Result save(@RequestBody GwScript gwScript) {
        return decide(gwScriptService.save(gwScript));
    }

    /**
     * 修改脚本管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/ipaas/script/edit','ipaas:script:edit')")
    public ModelAndView edit(String id, ModelMap map) {
        GwScript gwScript =gwScriptService.getById(id);
        map.put("gwScript", gwScript);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改脚本管理
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/ipaas/script/edit','ipaas:script:edit')")
    public Result update(@RequestBody GwScript gwScript) {
        return decide(gwScriptService.updateById(gwScript));
    }

    /**
     * 删除脚本管理
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/ipaas/script/remove','ipaas:script:remove')")
    public Result batchRemove(String ids) {
        return decide(gwScriptService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除脚本管理
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/ipaas/script/remove','ipaas:script:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(gwScriptService.removeById(id));
    }
}